<template>
  <!-- 余额页面 -->
  <div class="balance">
    <van-nav-bar title="我的余额" left-arrow @click-left="onClickLeft" />
    <div class="mymoney">
      <span>账户余额(元)</span>
      <strong>{{ userInfo.balance }}</strong>
    </div>
    <div class="button">
      <van-button
        round
        type="success"
        size="large"
        color="#666EE8"
        @click="showPopup"
        >立即充值</van-button
      >
    </div>
    <div class="instructions">
      <p>充值说明</p>
      <span v-for="(span, index) in data" :key="index">{{ span }}</span>
    </div>
  </div>
  <!-- 弹出层 -->
  <van-popup
    v-model:show="show"
    closeable
    :style="{ height: '56%', width: '80%' }"
  >
    <div class="recharge">
      <h3>余额充值</h3>
      <span>充值金额</span>
      <ul class="moneyList">
        <li v-for="(item, index) in list" :key="index">
          <van-button type="default" @click="handleInput(item)"
            >￥{{ item }}</van-button
          >
        </li>
      </ul>
      <div>
        <input placeholder="请输入充值金额" v-model="value" />
      </div>
      <span>充值方式</span>
      <ul class="pay">
        <li>
          <van-button type="default">微信支付</van-button>
        </li>
        <li>
          <van-button type="default">支付宝支付</van-button>
        </li>
      </ul>
      <van-button round size="large" color="#666EE8" @click="pay(value)"
        >立即充值</van-button
      >
    </div>
  </van-popup>
</template>

<script>
import { Overlay } from 'vant'
import { Toast } from 'vant'
import 'vant/es/toast/style'
export default {
  name: 'balance',
  data() {
    return {
      list: [10, 20, 50, 100, 200, 500],
      data: [
        '1.充值金额用于平台购买课程消费，不可提现，不可转让。',
        '2.如充值遇到问题请联系客服。',
        '3.充值方式可选择支付宝或微信。',
        '......',
      ],
      show: false,
      value: '',
    }
  },
  components: {
    Overlay,
    Toast,
  },
  computed: {
    userInfo() {
      // console.log('bal:', this.$store.getters.userInfo)m
      console.log(this.$store.getters['userModule/userInfo'])
      // return this.$store.getters.userInfo
      return this.$store.getters['userModule/userInfo']
    },
  },
  methods: {
    onClickLeft() {
      this.$router.push('/user')
    },
    showPopup() {
      this.show = true
    },
    pay(value) {
      if (value < 0 || value === '') {
        Toast.fail('重新输入金额')
        return
      }
      this.$store.commit('userModule/pay', value)
      Toast.success('充值成功')
      this.show = false
      this.value = ''
      // console.log(this.$store.getters['userModule/userInfo'])
    },
    handleInput(item) {
      this.value = item
      console.log(item)
    },
  },
}
</script>

<style lang="less" scoped>
@import '@/style/index.less';
@import '@/style/common.less';
.balance {
  background: @242;
  height: 100vh;
  .mymoney {
    background: @666;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    height: 24vh;
    color: @cf;
    strong {
      font-size: 2rem;
      margin-top: 0.5rem;
    }
  }
  .button {
    margin: 0 8%;
    margin-top: 12%;
  }
  .instructions {
    padding: 0 2%;
    span {
      display: block;
    }
  }
}
.recharge {
  box-sizing: border-box;
  width: 96%;
  height: 100%;
  margin: 0 2%;
  h3,
  p {
    margin: 0;
    padding: 0;
  }
  h3 {
    padding-top: 0.5rem;
  }
  span {
    display: block;
  }
  input {
    background: @242;
    border: 0;
    width: 96%;
    height: 5vh;
    margin: 1vh 2%;
  }
  .moneyList {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    li {
      width: 32%;
      margin-top: 1vh;
      display: flex;
      justify-content: center;
    }
  }
  .pay {
    display: flex;
    justify-content: space-between;
    margin-bottom: 2vh;
    li {
      width: 50%;
      margin-top: 1vh;
      display: flex;
      justify-content: center;
    }
  }
}
</style>
